<?php
require ("../../job/common/pdata_mysql.php");
require ("../../job/common/global.php");
//读取数据库并且形成xml文件;
function selectdata($database, $table, $select, $where, $orderby, $limit) {
	//echo $xmlstring;
	$conn = mysql_pconnect(HOSTNAME, USERNAME, PASSWORD);
	$orderby = "";
	if (!$limit)
		$limit = "LIMIT 0,10";
	//如何$limit为空，则默认显示前10个。
	mysql_select_db($database);

	//$sql = "SELECT "$select FROM $table[ WHERE $where][ ORDER BY $orderby][ LIMIT $limit]
	$sql = "select " . $select . " from " . $table . " " . $where . " " . $orderby . " " . $limit;
//	echo $sql;
	$rs = mysql_query($sql, $conn) or die(mysql_error());
	$num = mysql_num_fields($rs);

//	$xmlstring 	= "<<<XML ";
	$xmlstring 	="<?xml version=\"1.0\" encoding=\"UTF-8\"?>
				  	<".$database.">";
	while ($row = mysql_fetch_array($rs)) {
		$xmlstring .= "<" . $table . ">";
		for ($i = 0; $i < $num; $i++) {
			$proper = mysql_fetch_field($rs, $i);
			$xmlstring .= "<" . $proper -> name . ">";
			$xmlstring .= $row[$i];
			$xmlstring .= "</" . $proper -> name . ">";
		}
		$xmlstring .= "</" . $table . ">";
	}
	$xmlstring .= "</" . $database . ">";
//	$xmlstring .= "XML";
//	echo $xmlstring;
	return $xmlstring;
}

//给定xml文件后，将xml内容插入数据库中
function insertdata($xmlstring) {
	$xml = simplexml_load_string($xmlstring);
	$database = $xml;
	$table = $database->children();

	echo "database:".$database->getName()."<br />";
	echo "table:".$table->getName()."<br />";
	
	foreach($table as $table)
	{
		//INSERT INTO $table (row1,row1) VALUES (num1,num2);
		$sql = "INSERT INTO ".$table->getName()." (";
		foreach($table->children() as $dataname)
		{
			$sql .=$dataname->getName().",";
		}
		$sql .=")";
		$sql .=" VALUES (";
		$sql .="";
		foreach($table->children() as $data)
		{
			$sql .="'".$data."',";
		}
		$sql .=")";
	
		$sql = str_replace(',)',')',$sql);
		echo $sql."<br />";
	}

//	$data = $xml->children();
//	foreach($data as $array){
//		foreach($array->children() as $child)
//		{
//			echo $child->getName() .":".$child."<br />";
//		}
//	}
}

function updatedata($xmlstring,$whereupdate) {
	$xml = simplexml_load_string($xmlstring);
	$database = $xml;
	$table = $database->children();

	echo "database:".$database->getName()."<br />";
	echo "table:".$table->getName()."<br />";
	
	
		//UPDATE $table SET row1 = num1,row2 = num2 WHERE row = num;
	foreach($table as $table)
	{
		$sql = "UPDATE ".$table->getName()." SET ";
		foreach($table->children() as $data)
		{
			if($data->getName() != $whereupdate)
			{
				$sql .=$data->getName()."='".$data."', ";
			}
			else
			{
				$where = ", WHERE ".$data->getName()." = ".$data;
			}
		}
		$sql = $sql.$where;
		$sql = str_replace(', ,',' ',$sql);
		echo $sql."<br />";
	}

//	$data = $xml->children();
//	foreach($data as $array){
//		foreach($array->children() as $child)
//		{
//			echo $child->getName() .":".$child."<br />";
//		}
//	}
}

$database = "pdata";
$table = "project";  //postdoc,project,itemdetail
$select = "pid,projectname";
$where = "";
$orderby = "";
$limit = "";

$xmlstring = selectdata($database, $table, $select, $where, $orderby, $limit);
// echo $xmlstring."\n";
insertdata($xmlstring);
updatedata($xmlstring,"pid");
//displaystation();

//if(isset($_GET['query']) )
//	{if($_GET['query']=="")
//		$query = "";
//	else
//		$query = " WHERE stname LIKE '%".$_GET['query']."%'";}
//else
//	$query = "";
////echo $stid;
//$xmlstring = selectstations($query);
////echo $xmlstring;
////echo $xmlstring;
//$xml = simplexml_load_string($xmlstring) ;
////var_dump($xml);
////print_r($xml);
//$ss = $xml->station;
//foreach($ss as $array){
//	//echo $ss->getName()."<br />";
//	foreach($array->children() as $child)
//	{
//		echo $child->getName() .":".$child;
//		$st[$child->getName()]=$child;
//	}
//}
?>
